package com.wangxiaonan.coupon.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: wangxiaonan
 * @Date: 2019/1/10
 **/
@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket apiOne() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("coupon")
                .apiInfo(couponInfo())
                .enable(true) // 控制是否可用
                .select()
                .paths(couponPaths())
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))// 匹配给定注解的api
                .build();
    }

    private ApiInfo couponInfo() {
        return new ApiInfoBuilder()
                .title("Coupon Dome")
                .description("Coupon receive")
                .termsOfServiceUrl("https://gitee.com/wangxiaonan")
                .contact(new Contact("wxn", "https://gitee.com/wangxiaonan/coupon-demo", "wangxiaonan_0553@163.com"))
                .version("1.0")
                .build();
    }

    private Predicate<String> couponPaths() {
        return Predicates.or(PathSelectors.regex("/coupon.*"));// 根据url分组
    }
}
